const app = getApp();

Page({
  data: {
    riderInfo: {
      id: null,
      name: '',
      phone: '',
      avatar: '',
      vehicleType: null,
      vehicleTypeName: '',
      vehiclePlate: '',
      status: null,
      statusName: '',
      createTime: '',
      updateTime: ''
    },
    loading: false,
    isEditing: false
  },

  onLoad() {
    wx.setNavigationBarTitle({
      title: '个人信息'
    });
    this.fetchRiderInfo();
  },

  onShow() {
    // 每次显示页面时刷新数据
    this.fetchRiderInfo();
  },

  // 获取骑手信息
  fetchRiderInfo() {
    const ip = app.globalData.userIPInfo;
    const token = app.globalData.ridertoken;
    const riderId = app.globalData.riderId;

    if (!token || !riderId) {
      wx.showToast({ title: '请先登录', icon: 'none' });
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
      return;
    }

    this.setData({ loading: true });

    wx.request({
      url: `${ip}/api/riders/info?id=${riderId}`,
      method: 'GET',
      header: {
        'Authorization': 'Bearer ' + token,
        'content-type': 'application/json'
      },
      success: (res) => {
        this.setData({ loading: false });
        
        if (res.data.code === 200) {
          const data = res.data.data;
          this.setData({
            riderInfo: {
              id: data.id,
              name: data.name,
              phone: data.phone,
              avatar: data.avatar || '/assets/icons/default_avatar.png',
              vehicleType: data.vehicleType,
              vehicleTypeName: data.vehicleTypeName,
              vehiclePlate: data.vehiclePlate,
              status: data.status,
              statusName: data.statusName,
              createTime: data.createTime,
              updateTime: data.updateTime
            }
          });
        } else {
          wx.showToast({ 
            title: res.data.message || '获取信息失败', 
            icon: 'none' 
          });
        }
      },
      fail: () => {
        this.setData({ loading: false });
        wx.showToast({ title: '网络请求失败', icon: 'none' });
      }
    });
  },

  // 复制手机号
  copyPhone() {
    wx.setClipboardData({
      data: this.data.riderInfo.phone,
      success: () => {
        wx.showToast({ title: '手机号已复制', icon: 'success' });
      }
    });
  },

  // 复制车牌号
  copyPlate() {
    if (this.data.riderInfo.vehiclePlate) {
      wx.setClipboardData({
        data: this.data.riderInfo.vehiclePlate,
        success: () => {
          wx.showToast({ title: '车牌号已复制', icon: 'success' });
        }
      });
    }
  },

  // 修改密码
  changePassword() {
    wx.navigateTo({
      url: '/pages/change-password/index'
    });
  },



  // 格式化时间
  formatTime(timeStr) {
    if (!timeStr) return '暂无';
    const date = new Date(timeStr);
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    const hour = date.getHours().toString().padStart(2, '0');
    const minute = date.getMinutes().toString().padStart(2, '0');
    return `${year}-${month}-${day} ${hour}:${minute}`;
  },

  // 获取状态颜色
  getStatusColor(status) {
    switch(status) {
      case 1: return '#52c41a'; // 空闲 - 绿色
      case 2: return '#fa8c16'; // 忙碌 - 橙色
      case 3: return '#f5222d'; // 离线 - 红色
      default: return '#d9d9d9'; // 未知 - 灰色
    }
  },

  // 获取车辆类型图标
  getVehicleIcon(vehicleType) {
    switch(vehicleType) {
      case 1: return '🛵'; // 电动车
      case 2: return '🚲'; // 自行车
      case 3: return '🏍️'; // 摩托车
      case 4: return '🚗'; // 汽车
      default: return '🚚'; // 默认
    }
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.fetchRiderInfo();
    wx.stopPullDownRefresh();
  }
});
